package richbar.com.richbar.activity.product;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;
import com.common.base.BaseActivity;
import com.common.util.LogUtil;
import com.common.util.StringUtil;
import com.common.util.ToastUtil;
import com.gxz.PagerSlidingTabStrip;
import com.okhttp.network.HttpUtils;
import com.okhttp.network.MyGson;
import java.util.ArrayList;
import java.util.List;
import butterknife.ButterKnife;
import butterknife.OnClick;
import richbar.com.richbar.R;
import richbar.com.richbar.activity.login.LoginAc;
import richbar.com.richbar.adapter.product.ItemTitlePagerAdapter;
import richbar.com.richbar.base.BaseApplication;
import richbar.com.richbar.bean.AddCarResult;
import richbar.com.richbar.bean.GoodsInfoResult;
import richbar.com.richbar.fragment.ShoppingCartFragment;
import richbar.com.richbar.fragment.product.ProductCommentFragment;
import richbar.com.richbar.fragment.product.ProductDetailsFragment;
import richbar.com.richbar.fragment.product.ProductInfoFragment;
import richbar.com.richbar.util.NetWorkUtil;
import richbar.com.richbar.util.Util;
import richbar.com.richbar.view.pop.TypePop;

/**
 * Created by QiYang on 2018/11/27.
 */

public class ProductDetailsAc extends BaseActivity {
    public TextView tv_title;
    public ViewPager vp_content;
    private TextView car_num;
    public PagerSlidingTabStrip psts_tabs;
    private List<Fragment> fragmentList = new ArrayList<>();
    private String id;
    private ProductInfoFragment productInfoFragment;
    private GoodsInfoResult.DataEntity.InfoEntity bean;
    private String selectIds;
    private String selectValues;
    private String selectPrice;
    private String selectNum;
    private int carNum;
    private String pic="";
    @Override
    public int initLayout() {
        return R.layout.ac_product_details;
    }

    @Override
    public void initTitle() {
        findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
        id = getBundle().getString("id");
        pic = getBundle().getString("pic");
        tv_title = findViewById(R.id.tv_title);
        vp_content = findViewById(R.id.vp_content);
        psts_tabs = findViewById(R.id.psts_tabs);
        car_num = findViewById(R.id.car_num);
        productInfoFragment = ProductInfoFragment.newInstance(id,pic);
        fragmentList.add(productInfoFragment);
        fragmentList.add(ProductDetailsFragment.newInstance(id));
        fragmentList.add(new ProductCommentFragment());
        vp_content.setAdapter(new ItemTitlePagerAdapter(getSupportFragmentManager(),
                fragmentList, new String[]{"商品", "详情", "评价"}));
        vp_content.setOffscreenPageLimit(3);
        psts_tabs.setViewPager(vp_content);
    }


    @Override
    public void initData() {
        productInfoFragment.setCallback(new ProductInfoFragment.Callback() {
            @Override
            public void callback(GoodsInfoResult.DataEntity.InfoEntity bean) {
                ProductDetailsAc.this.bean = bean;
                int carNums = bean.getCart_num();
                if (carNums > 0){
                    carNum=bean.getCart_num();
                    car_num.setText(carNums + "");
                    car_num.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void attrs(boolean isSelect,String ids, String values,String price, String num) {
                selectIds = ids;
                selectValues = values;
                selectPrice = price;
                selectNum = num;
            }
        });
    }

    public void setViewPagerPosition(int pos){
        vp_content.setCurrentItem(pos);
    }

    public String getGoodsId(){
        return id;
    }


    @OnClick({R.id.link, R.id.shop, R.id.shop_car, R.id.add_shop_car, R.id.buy})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.link:
                break;
            case R.id.shop:
                if (null == bean) {
                    return;
                }
                start(ShopAc.class, new BaseActivity.OnBundleListener() {
                    @Override
                    public Bundle onSetBundle(Bundle bundle) {
                        bundle.putString("id", bean.getGoods_sellerid() + "");
                        return bundle;
                    }
                });
                break;
            case R.id.shop_car:
                if (BaseApplication.uid.equals("")) {
                    start(LoginAc.class);

                } else {
                    start(ShopCarAc.class);
                }
                break;
            case R.id.add_shop_car:
                if (BaseApplication.uid.equals("")) {
                    start(LoginAc.class);
                } else {
                    car_num.setVisibility(View.VISIBLE);
                    addCar();
                }

                break;
            case R.id.buy:
                if (BaseApplication.uid.equals("")) {
                    start(LoginAc.class);
                } else {
                    if (null == bean) {
                        return;
                    }
                    start(FirmOrderAc.class, new OnBundleListener() {
                        @Override
                        public Bundle onSetBundle(Bundle bundle) {
                            bundle.putString("id", bean.getGoods_id() + "");
                            bundle.putString("ids", selectIds);
                            bundle.putString("price", selectPrice);
                            bundle.putString("num", selectNum);
                            bundle.putString("values", selectValues);
                            bundle.putString("pic", pic);
                            return bundle;
                        }
                    });
                    break;
                }
        }
    }

    private void addCar() {
        TypePop pop = new TypePop(this, pic,bean);
        pop.show(new TypePop.Callback() {
            @Override
            public void onBuy(final String id, final String value, final String price, final String num) {
                LogUtil.e("ADADDADAD",id);
                start(FirmOrderAc.class, new OnBundleListener() {
                    @Override
                    public Bundle onSetBundle(Bundle bundle) {
                        bundle.putString("id", bean.getGoods_id()+"");
                        bundle.putString("ids", id);
                        bundle.putString("price", price);
                        bundle.putString("num", num);
                        bundle.putString("values", value);
                        bundle.putString("pic", pic);
                        return bundle;
                    }
                });
            }

            @Override
            public void onAdd(String id, String values, String price, String num) {
                addCar(id,values,price,num);
                LogUtil.e("ADADDADAD",id);
            }
        });

    }

    private void addCar(String ids, String values, String price, String num) {
        NetWorkUtil.addCart(BaseApplication.uid, id, num,price,ids, Util.md5(), new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                final AddCarResult result = MyGson.getInstance().fromJson(data, AddCarResult.class);
                if (checkIsOk(result)){
                    if (!StringUtil.isEmpty(result.getData().getCart_id())){
                        ToastUtil.showToast(ProductDetailsAc.this,"添加成功");
                        carNum ++;
                        car_num.setText(carNum + "");

                        ShoppingCartFragment.sendBraodcast(ProductDetailsAc.this);
                    }else {
                        ToastUtil.showToast(ProductDetailsAc.this,"添加失败");
                    }
                }
            }
        });
    }
}
